//
//  CacheShareManager.swift
//  Created by ios on 2019/5/21.
//  Copyright © 2019 ios. All rights reserved.
//

import Foundation
import UIKit

//app infor
class XGL_AppBasicInforManager{
    let storage = CodableStorage()
    var infor: AppBasicInfor{
        var userData = AppBasicInfor()
        do {
            let cached: AppBasicInfor = try storage.fetch(for: XGL_AppInforCacheKey.share.appInforKey)
            userData = cached
        } catch {
            print(error)
        }
        return userData
    }
    
    func saveInfor(inforData: AppBasicInfor){
        do {
            try storage.save(inforData, for: XGL_AppInforCacheKey.share.appInforKey)
        } catch  {
            print(error)
        }
    }
    
    func removeInfor() {
        do {
            try storage.delete(for: XGL_AppInforCacheKey.share.appInforKey)
        } catch  {
            print(error)
        }
    }
    
    static let shared = XGL_AppBasicInforManager()
    private init(){}
}

class XGL_userDataCacheManager{
    let storage = CodableStorage()
    var token: AppToken{
        var userData = AppToken()
        do {
            let cached: AppToken = try storage.fetch(for: XGL_AppInforCacheKey.share.userTokenKey)
            userData = cached
        } catch {
            print(error)
        }
        return userData
    }
    
    func saveInfor(inforData: AppToken){
        do {
            try storage.save(inforData, for: XGL_AppInforCacheKey.share.userTokenKey)
        } catch  {
            print(error)
        }
    }
    
    func removeInfor() {
        do {
            try storage.delete(for: XGL_AppInforCacheKey.share.userTokenKey)
        } catch  {
            print(error)
        }
    }
    
    static let shared = XGL_userDataCacheManager()
    private init(){}
}

class XGL_WaterfallImageCacheManager{
    let storage = CodableStorage()
    var infor: WaterFallImageCache{
        var data = WaterFallImageCache()
        do {
            let cached: WaterFallImageCache = try storage.fetch(for: XGL_AppInforCacheKey.share.appInforKey)
            data = cached
        } catch {
            print(error)
        }
        return data
    }
    
    func saveInfor(inforData: WaterFallImageCache){
        do {
            try storage.save(inforData, for: XGL_AppInforCacheKey.share.appInforKey)
        } catch  {
            print(error)
        }
    }
    
    func removeInfor() {
        do {
            try storage.delete(for: XGL_AppInforCacheKey.share.appInforKey)
        } catch  {
            print(error)
        }
    }
    
    static let shared = XGL_WaterfallImageCacheManager()
    private init(){}
}

class ChatMessageCacheManager{
    let storage = CodableStorage()
    var infor: MessageCache{
        var data = MessageCache()
        do {
            let cached: MessageCache = try storage.fetch(for: XGL_AppInforCacheKey.share.userChatKey)
            data = cached
        } catch {
            print(error)
        }
        return data
    }
    
    func saveInfor(inforData: MessageCache){
        do {
            try storage.save(inforData, for: XGL_AppInforCacheKey.share.userChatKey)
        } catch  {
            print(error)
        }
    }
    
    func removeInfor() {
        do {
            try storage.delete(for: XGL_AppInforCacheKey.share.userChatKey)
        } catch  {
            print(error)
        }
    }
    
    static let shared = ChatMessageCacheManager()
    private init(){}
}
